package com.example.hhh.repository;

import com.example.hhh.model.WorkOrder;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;


import java.time.LocalDateTime;
import java.util.List;

@Repository
public interface WorkOrderRepository extends JpaRepository<WorkOrder, Integer> {

    // 根据工单号查询
    WorkOrder findByOrderNumber(String orderNumber);

    // 根据客户ID查询工单
    List<WorkOrder> findByCustomer_CustomerId(Integer customerId);

    // 根据业务类型查询工单
    List<WorkOrder> findByBusiness_BusinessType(String businessType);

    // 根据处理人员查询工单
    List<WorkOrder> findByAssignedStaff_StaffId(Integer staffId);

    List<WorkOrder> findByCustomerId(Integer customerId);

    // 根据 assignedTo（可能是员工ID或对象）查询工单
    List<WorkOrder> findByAssignedTo(Integer assignedTo);  // 如果 assignedTo 是 ID


    // 根据状态查询工单
    List<WorkOrder> findByOrderStatus(Integer status);

    // 查询超时未完成的工单
    @Query("SELECT wo FROM WorkOrder wo WHERE wo.orderStatus < 2 AND wo.expectedCompleteTime < :currentTime")
    List<WorkOrder> findOverdueOrders(@Param("currentTime") LocalDateTime currentTime);

    // 根据优先级和状态组合查询
    List<WorkOrder> findByPriorityAndOrderStatus(Integer priority, Integer status);

    // 统计某客户的工单数量
    long countByCustomer_CustomerId(Integer customerId);
}